package com.exercises;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Exercise1734 {
    public static void main(String[] args) {
        System.out.println(JSONObject.toJSONString(decode(new int[]{6, 5, 4, 6})));
    }

    public static int[] decode(int[] encoded) {
        int length = encoded.length;
        int[] result = new int[length + 1];
        int allXOR = 0, expectFirst = 0;
        for (int i = 1; i <= length + 1; i++) {
            allXOR ^= i;
        }

        for (int i = 1; i < encoded.length; i += 2) {
            expectFirst ^= encoded[i];
        }
        result[0] = allXOR ^ expectFirst;
        for (int i = 1; i < result.length; i++) {
            result[i] = result[i - 1] ^ encoded[i - 1];
        }
        return result;
    }
}
